package com.stoneapple.timesheet.ProjectServlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.stoneapple.timesheet.dao.ProjectDao;
import com.stoneapple.timesheet.entity.Project;;


@SuppressWarnings( "serial" )
public class ProjectServlet  extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		
		if (user == null) {
			String loginUrl = userService.createLoginURL(req.getRequestURI());
			resp.sendRedirect(loginUrl);
			
		} else {
			
			
			ProjectDao projectDao = ProjectDao.getInstance();
			List<Project> project = projectDao.getProjectListByUserId(user.getUserId());
			
			req.setAttribute("projects", project);

			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/showProject.jsp");
			dispatcher.forward(req, resp);
		}
	}

}
